package com.cskaoyan.com.collection.treemap;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeMap;

public class DemoTreeMap2 {
    public static void main(String[] args) {


//        TreeMap<User, String> map = new TreeMap<>();
//        map.put(new User(), "zs");
//        map.put(new User(), "zs");
//        map.put(new User(), "zs");
//        map.put(new User(), "zs");
//        System.out.println(map.size());


//        TreeMap(Comparator<? super K> comparator)
//        构造一个新的、空的树映射，该映射根据给定比较器进行排序。
//

        TreeMap<User, String> map = new TreeMap<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                int com = o1.getName().compareTo(o2.getName());

                com = com == 0 ? o1.getAge() - o2.getAge() : com;

                return com;
            }
        });
        map.put(new User("zs", 18), "zs");


    }
}
// Bean类
class User{
    private String name;
    private int age;

    public User() {
    }
    public User(String name) {
        this.name = name;
    }
    public User(int age) {
        this.age = age;
    }
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
//class User implements Comparable<User>{
//
//    @Override
//    public int compareTo(User o) {
//        return 0;
//    }
//}
